<template>
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" @filterChange="handleSearch" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="toosChooseRef"
      :columns="columns"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :datakey="'rows'"
      :onSyncTableData="handleSyncTableData"
      :onRowSelectChange="selectListAdd"
      :onColumnsChange="columns => (this.columns = columns)"
    >
    </FilterTable>
    <div class="btnWrap">
      <el-button type="primary" @click="closeDrawer">取消</el-button>
      <el-button type="primary" @click="saveHandler">保存</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { queryToolsInfo } from '@/api/part/tools';
import { getPinpSelect } from '@/api/repair/repairReception/customer/brand.js';
import { exportExcelFile, arrayObjDeWeight } from '@/utils/index';
import { queryUnitCode } from '@/api/repair/batchEventManagement/activityDistribution';
import { confirmAction, notifyAction } from '@/utils';
import { getStroageList } from '@/api/part/partOutbound/partBreak';

export default {
  mixins: [dictionary],
  mounted() {
    this.getUnit();
    // this.getBrand();
    this.searchstore();
  },
  data() {
    return {
      topFilterList: this.createTopFIlterList(),
      columns: this.createTableColumns(),
      fetchapi: queryToolsInfo,
      selectList: [],
      fetchParams: { xhrAbort: true },
      showPanel: false,
      syncTableData: [] // 同步的table数据
    };
  },
  methods: {
    createTopFIlterList() {
      return [
        {
          type: 'INPUT',
          label: '工具代码',
          fieldName: 'partNo'
        },
        {
          type: 'INPUT',
          label: '工具名称',
          fieldName: 'partName'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '工具代码',
          dataIndex: 'partNo',
          sorter: true
        },
        {
          title: '工具名称',
          dataIndex: 'partName',
          sorter: true
        },
        {
          title: '仓库名称',
          dataIndex: 'storageCode',
          dictItems: [],
          sorter: true
        },
        {
          title: '库位',
          dataIndex: 'storagePositionCode',
          sorter: true
        },
        {
          title: '单位',
          dataIndex: 'unitCode',
          filterType: 'checkbox',
          dictItems: [],
          sorter: true
        },
        // {
        //   title: '品牌',
        //   dataIndex: 'brand',
        //   filterType: 'checkbox',
        //   dictItems: [],
        //   sorter: true
        // },
        {
          title: '数据来源',
          dataIndex: 'downTag',
          filterType: 'checkbox',
          dictItems: [
            { text: '店端自建', value: '10041002' },
            { text: '厂端下发', value: '10041001' }
          ],
          sorter: true
        },
        {
          title: '账面库存',
          dataIndex: 'stockQuantity',
          filterType: 'number',
          sorter: true
        },
        {
          title: '借出数量',
          dataIndex: 'lendQuantity',
          filterType: 'number',
          sorter: true
        }
      ];
    },
    collapseHandle() {
      this.nextTick(() => {
        this.refs.table.EXECUTE_RESET_HEIGHT();
      });
    },
    // 点击搜索
    handleSearch(e) {
      this.fetchParams = Object.assign({ xhrAbort: false }, e);
      this.fetchParams = { ...this.fetchParams };
    },
    // 同步table数据
    handleSyncTableData(list) {
      this.syncTableData = list;
    },
    closeDrawer() {
      this.$emit('close');
    },
    saveHandler() {
      this.$emit('confirm', this.selectList);
    },
    async getUnit() {
      const res = await queryUnitCode();
      console.log('getUnit res::', res);
      let data = arrayObjDeWeight(res.data, 'UNIT_CODE');
      this.columns.find(x => x.dataIndex == 'unitCode').dictItems = data.map(x => ({ value: x.UNIT_CODE + '', text: x.UNIT_NAME }));
    },
    selectListAdd(val) {
      this.selectList = val;
    },
    async searchstore() {
      const res = await getStroageList();
      this.codeName = this.columns.find(x => x.dataIndex == 'storageCode').dictItems = this.columns.find(x => x.dataIndex == 'storageCode').filterItems = this.columns.find(
        x => x.dataIndex == 'storageCode'
      ).editItems = res.data.map(x => ({
        value: x.STORAGE_CODE,
        text: x.STORAGE_NAME
      }));
    },
    // 查询所有品牌
    async getBrand() {
      const res = await getPinpSelect();
      console.log('brandres::', res);
      let brandItem = this.columns.find(x => x.dataIndex === 'brand');
      brandItem.dictItems = res.data.map(x => ({ text: x.brandName, value: x.brandCode + '' }));
      // 初始化车系列表
      // this.getSeries();
    }
  }
};
</script>

<style lang="less" scoped>
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
